<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Memcache
 *
 * @author ananda
 */
class Data_Collection_Memcache extends Data_Collection
{
    protected $cursor;
    protected $index;
    protected $key_field = null;

    public function init() {
        $this->index  = array_keys($this->handler);
        $this->rewind();
    }

    /**
     * 迭代器接口
     * @return mixed
     */
    public function current()
    {
        return $this->handler[$this->cursor];
    }

    /**
     * 迭代器接口
     * @return mixed
     */
    public function key()
    {
        if ($this->key_field) {
            //暂时如果找不到字段导致的错误不做特别处理
            return $this->handler[$this->cursor][$this->key_field];
        } else {
            return $this->cursor;
        }
    }

    /**
     * 迭代器接口
     * @return void
     */
    public function next()
    {
        $this->cursor = next($this->index);
    }

    /**
     * 迭代器接口
     * @return void
     */
    public function rewind()
    {
        reset($this->index);
        $this->cursor = current($this->index);
    }

    /**
     * 迭代器接口
     * @return boolean
     */
    public function valid()
    {
        return $this->cursor !== false && isset($this->handler[$this->cursor]);
    }

    public function rowCount()
    {
        return count($this->handler);
    }

    public function getArray()
    {
        return $this->handler;
    }
}
